package struts.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.myService.panduan.panDuanService;
import service.myService.panduan.panDuanServiceImpl;
import service.vo.MessageVo;
import struts.common.panDuan;
import struts.form.ActionForm;
import struts.form.FullForm;
import struts.form.XmlBean;
import business.panDuanAction;

public class ActionServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		//String name = request.getParameter("name");
//        String url ;
//        panDuanService paduan = new panDuanServiceImpl();
//        Map map = paduan.excute(request);
//        url = (String)map.get("url");
//		request.getRequestDispatcher(url).forward(request, response);	
//		String name = request.getParameter("name");
		
		String path = this.getPath(request.getServletPath());//获取请求头
		Map<String,XmlBean> map = 
				(Map<String, XmlBean>) this.getServletContext().getAttribute("struts");
		XmlBean xml = map.get(path);
		String formClass = xml.getFormClass();//formClass就是要操作的对象
		ActionForm form = FullForm.full(formClass, request);//利用反射（组装数据）得到类,并且此时类已经有数据
		String actionType = xml.getActionType();
		Action action = null;
		String url = "";
		try {
			Class clazz = Class.forName(actionType);
			action = (Action) clazz.newInstance();//接口指向实类
			url = action.execute(request, response,form, xml.getActionForward());//得到struts中指向的url路径
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("严重：控制器异常！");
		}
		request.getRequestDispatcher(url).forward(request, response);	
		
	}
	
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		this.doGet(request, response);
	}
    
   private String getPath(String path){
	   return path.split("\\.")[0];
   }

    
    
    
    
    
    

}
